<template>
  <div class="curriculum">
    <div class="intersection">
      <div class="left">
        <div class="list" v-for="(item, index) in intersectionList" :key="index"
          :class="{ acitve: index == intersectionActive }" @click="intersectionClick(index, item)">
          {{ item.value }}
        </div>
      </div>

      <!-- <div class="right">
        <div class="button" @click="addHit()">新增</div>
      </div> -->
    </div>

    <div class="common">
      <div class="common-input">
        <el-input v-model="commonValue" placeholder="课程名称/课程ID" clearable></el-input>
      </div>

      <div class="common-input">
        <el-select v-model="commonUpdateValue" placeholder="请选择课程分类" clearable>
          <el-option v-for="item in classify1Options" :key="item.courseClassifyId" :label="item.courseClassifyName"
            :value="item.courseClassifyId">
          </el-option>
        </el-select>
      </div>
      <!-- 
      <div class="common-input">
        <el-select v-model="commonStatusValue" placeholder="请选择状态" clearable>
          <el-option v-for="item in commonStatusOptions" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </div> -->

      <div class="common-input">
        <el-select v-model="commonRecommendValue" placeholder="请选择是否被推荐" clearable>
          <el-option v-for="item in commonRecommendOptions" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </div>

      <div class="common-input">
        <el-date-picker v-model="commonDate" type="daterange" range-separator="至" start-placeholder="开始日期"
          end-placeholder="结束日期" format="yyyy-MM-dd" value-format="yyyy-MM-dd">
        </el-date-picker>
      </div>

      <div class="common-button" @click="searchHit">搜索</div>
      <div class="common-button" @click="replacementHit" style="  background: #999999;">重置</div>
      <div class="common-add" @click="addHit()">新增</div>
    </div>

    <div class="table" v-loading="loading">
      <el-table @sort-change="sortChange" :data="tableData" style="width: 100%" border :header-cell-style="{
        color: '#333333',
        backgroundColor: '#f3f3f3',
      }">
        <el-table-column prop="courseId" label="课程ID" width="100">
        </el-table-column>
        <el-table-column prop="courseName" label="课程名称" width="300">
        </el-table-column>
        <el-table-column prop="" label="课程图片" width="200">
          <template slot-scope="scope">
            <img class="imgs" :src="scope.row.courseIcon" alt="" />
          </template>
        </el-table-column>
        <el-table-column prop="courseClassifyName" label="课程分类名称" width="200">
        </el-table-column>
        <el-table-column prop="price" label="销售价（元）" width="200" :sortable="'custom'">
        </el-table-column>
        <el-table-column prop="saleNum" label="销售数量" sortable width="200">
        </el-table-column>
        <el-table-column prop="" label="是否被推荐" sortable width="200">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.isIndex == 0" type="info">否</el-tag>
            <el-tag v-if="scope.row.isIndex == 1" type="success">是</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="状态" width="200">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.isStatus == 0" type="info">草稿</el-tag>
            <el-tag v-else-if="scope.row.isStatus == 1" type="success">已上架</el-tag>
            <el-tag v-else-if="scope.row.isStatus == 2" type="warning">已下架</el-tag>
            <el-tag v-else-if="scope.row.isStatus == 3" type="danger">已回收</el-tag>
          </template>
        </el-table-column>

        <el-table-column prop="createTime" label="创建时间" sortable width="200">
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="200">
          <template slot-scope="scope">
            <div class="operation">
              <div class="list" @click.prevent="detailsHit(scope.row)">
                详情
              </div>

              <div class="list" @click.prevent="editHit(scope.row)">编辑</div>

              <div class="list" @click.prevent="deleteHit(scope.row)">删除</div>
            </div>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination">
        <el-pagination :current-page="page" @size-change="handleSizeChange" @current-change="handleCurrentChange"
          background layout="prev, pager, next" :total="tableDataTotal">
        </el-pagination>
      </div>
    </div>

    <!--删除弹出框-->
    <div class="delete">
      <el-dialog title="删除" :visible.sync="deleteVisible" :before-close="deleteClose">
        <div class="delete-title">是否确认删除？</div>
        <div class="delete-attachment">
          <div class="list" @click="deleteClose">取消</div>
          <div class="list" @click="deleteConfirm">确定</div>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { courseClassifyList, courseList, courseListDelete } from "@/api/course";

export default {
  data() {
    return {
      //------------------------
      intersectionList: [
        {
          key: 0,
          value: "全部",
          soucre: "",
        },
        {
          key: 1,
          value: "已上架",
          soucre: "1",
        },
        {
          key: 2,
          value: "已下架",
          soucre: "2",
        },
        {
          key: 3,
          value: "草稿",
          soucre: "0",
        },
        {
          key: 4,
          value: "已回收",
          soucre: "3",
        },
      ],
      intersectionActive: 0,
      //---------
      commonValue: "",
      //------------
      commonDate: [],
      commonStart: "",
      commonEnd: "",
      //--------
      commonUpdateOptions: [],
      commonUpdateValue: "",
      classifyOptions: [],
      classify1Options: [],
      commonUpdateShow: false,
      //----------------
      commonStatusValue: "",
      commonStatusOptions: [
        {
          value: "0",
          label: "草稿",
        },
        {
          value: "1",
          label: "已上架",
        },
        {
          value: "2",
          label: "已下架",
        },
        {
          value: "3",
          label: "已回收",
        },
      ],
      commonRecommendValue: "",
      commonRecommendOptions: [
        {
          value: "-1",
          label: "全部",
        },
        {
          value: "1",
          label: "是",
        },
        {
          value: "0",
          label: "否",
        },
      ],

      //-----------------------
      page: 1,
      limit: 10,
      tableData: [],
      tableDataTotal: 0,
      loading: false,
      commonObj: {},
      //--------------
      tabSort: 0, //该字段代表升序还是降序 假设：0正序 1倒序
      tabProp: 0, //prop绑定的字段名
      deleteVisible: false,
    };
  },
  created() {
    if (this.$route.query.index) {
      this.intersectionActive = this.$route.query.source
      this.commonStatusValue = this.$route.query.index
    }

    this.loading = true;
    this.courseClassifyListMet();

    this.courseListMet();
  },
  methods: {
    courseClassifyListMet() {
      let params = {
        isIndex: 1,
        pageSize: 1000,
        pageNo: this.page,
      };

      courseClassifyList(params).then((res) => {
        if (res.errorCode == null) {
          this.commonUpdateOptions = res.resultData.courseClassifyList;
          this.courseClassifyListMet1();
        }
      });
    },
    courseClassifyListMet1() {
      let params = {
        isIndex: 0,
        pageSize: 1000,
        pageNo: 1,
      };

      courseClassifyList(params).then((res) => {
        if (res.errorCode == null) {
          this.classify1Options = this.commonUpdateOptions.concat(res.resultData.courseClassifyList)
        }
      });
    },
    //-------
    courseListMet() {
      let params = {
        bannerId: "",
        keyWord: this.commonValue,
        courseClassifyId: this.commonUpdateShow == true ? '' : this.commonUpdateValue,
        isStatus: this.commonStatusValue,
        isIndex:
          this.commonRecommendValue == -1 ? "" : this.commonRecommendValue,
        startTime: this.commonStart,
        endTime: this.commonEnd,
        orderType: this.tabProp, // 排序字段 0无 1销售价 2销售数量 3创建时间
        orderBy: this.tabSort, // 排序类型 0正 1倒
        pageSize: this.limit,
        pageNo: this.page,
      };

      courseList(params).then((res) => {
        if (res.errorCode == null) {
          res.resultData.courseList.forEach((item, index) => {
            item.price = Number(item.price / 100).toFixed(2);
          });

          this.tableData = res.resultData.courseList;
          this.tableDataTotal = res.resultData.totalNum;
          this.loading = false;
        }
      });
    },
    timeData(date) {
      return date.toLocaleString("zh-CN", {
        year: "numeric",
        month: "2-digit",
        day: "2-digit",
        // hour: "2-digit",
        // minute: "2-digit",
        // second: "2-digit",
      });
    },
    searchHit() {
      if (this.commonDate.length > 0) {
        this.commonStart = this.timeData(new Date(this.commonDate[0]));
        this.commonEnd = this.timeData(new Date(this.commonDate[1]));
      } else {
        this.commonStart = "";
        this.commonEnd = "";
      }

      console.log('2222', this.classify1Options, this.commonUpdateValue)

      this.commonUpdateShow = false
      this.classify1Options.forEach((item, index) => {
        if (item.courseClassifyId == this.commonUpdateValue) {

          if (item.courseClassifyName == "全部") {
            this.commonUpdateShow = true
          }
        }
      })


      this.page = 1;
      this.loading = true;
      this.courseListMet();
    },
    replacementHit() {
      this.commonDate = [];
      this.commonStart = "";
      this.commonEnd = "";
      this.commonValue = "";

      this.commonUpdateValue = "";
      this.commonStatusValue = "";
      this.commonRecommendValue = "";
      this.tabProp = 0;
      this.tabSort = 0;

      this.page = 1;
      this.loading = true;
      this.courseListMet();
    },
    //-----------
    intersectionClick(index, item) {
      this.intersectionActive = index;
      this.commonStatusValue = item.soucre;

      if (this.$route.query.index) {
        this.$router.push({
          path: this.$route.path,
          query: {}
        });
      }

      this.page = 1;
      this.loading = true;
      this.courseListMet();
    },
    //------------------------------------------------
    sortChange(column) {
      // 当前列的排序  ascending 升序 descending 降序
      console.log("获取对象", column);
      if (column.order == "ascending") {
        this.tabSort = 0;
      } else if (column.order == "descending") {
        this.tabSort = 1;
      } else {
        this.tabSort = 0;
      }

      // 当前点击列名
      this.tabProp =
        column.prop == "price"
          ? 1
          : column.prop == "saleNum"
            ? 2
            : column.prop == "createTime"
              ? 3
              : 0;
      this.page = 1;
      this.loading = true;
      this.courseListMet();
    },

    //-------------------------------------------------
    addHit() {
      if (this.$store.state.premissions.infoRouterShow == false) {
        this.$message.error("暂无权限，请联系管理员开通");
        return;
      }
      this.$router.push({
        path: "/course/table/list-details",
        query: {
          soucre: 0,
        },
      });
    },
    editHit(item) {
      if (this.$store.state.premissions.infoRouterShow == false) {
        this.$message.error("暂无权限，请联系管理员开通");
        return;
      }
      this.$router.push({
        path: "/course/table/list-details",
        query: {
          soucre: 0,
          id: item.courseId,
        },
      });
    },
    detailsHit(item) {
      if (this.$store.state.premissions.infoRouterShow == false) {
        this.$message.error("暂无权限，请联系管理员开通");
        return;
      }
      this.$router.push({
        path: "/course/table/list-details",
        query: {
          soucre: 1,
          id: item.courseId,
        },
      });
    },
    //-----------
    deleteHit(item) {
      if (this.$store.state.premissions.infoRouterShow == false) {
        this.$message.error("暂无权限，请联系管理员开通");
        return;
      }
      this.commonObj = item;
      this.deleteVisible = true;
    },
    deleteClose() {
      this.deleteVisible = false;
    },
    deleteConfirm() {
      let params = this.commonObj.courseId;

      courseListDelete(params).then((res) => {
        if (res.errorCode == null) {
          this.$message.success("删除成功！");
          this.loading = true;
          this.page = 1;
          this.courseListMet();
          this.deleteVisible = false;
        }
      });
    },

    //---------
    handleSizeChange(val) {
      this.limit = val;
      this.loading = true;
      this.courseListMet();
    },
    handleCurrentChange(val) {
      this.page = val;
      this.loading = true;
      this.courseListMet();
    },
  },
};
</script>

<style lang="less" scoped>
@import url("@/assets/css/common.less");
@import url("@/assets/css/delete.less");

.curriculum {
  .intersection {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 10px;

    .left {
      display: flex;
      align-items: center;

      .list {
        width: 80px;
        height: 30px;
        font-size: 14px;
        display: flex;
        align-items: center;
        justify-content: center;
        border: 1px solid #ccc;
        cursor: pointer;
      }

      .acitve {
        background: #2a82e4;
        color: #fff;
      }
    }

    .right {
      .button {
        padding: 9px 20px;
        box-sizing: border-box;
        border-radius: 5px;
        background: #2a82e4;
        font-size: 14px;
        color: #fff;
        cursor: pointer;
      }
    }
  }

  .common {
    display: flex;
    align-items: center;
    margin-bottom: 10px;

    .common-input {
      margin-right: 10px;

      /deep/ .el-input {
        width: 250px;
      }
    }

    .common-input:last-child {
      margin-right: 0;
    }

    .common-button {
      padding: 9px 20px;
      box-sizing: border-box;
      border-radius: 5px;
      background: #2a82e4;
      font-size: 14px;
      color: #fff;
      margin-left: 20px;
      cursor: pointer;
    }

    .common-button:last-child {
      background: #999999;
    }

    .common-add {
      padding: 9px 20px;
      box-sizing: border-box;
      border-radius: 5px;
      background: #2a82e4;
      font-size: 14px;
      color: #fff;
      margin-left: 20px;
      cursor: pointer;
    }
  }
}
</style>